import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:umeng_push_demo/controller/app_controller.dart';
import 'package:umeng_push_demo/model/push_message_model.dart';
import 'package:umeng_push_demo/routes.dart';
import 'package:umeng_push_demo/view/components/home_list_cell.dart';

import 'components/home_header_view.dart';

/// FileName home_page
///
/// @Author 陈晓星
/// @Date 2024/10/25 08:33
///
/// @Description TODO

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;

  final AppController appController = Get.put(AppController());

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void toSettingPage() {
    AppRoutes.to(AppRoutes.setting);
  }

  void cleanMessageAction() async {
    await appController.deleteAllMessages();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text(
          "消息推送",
          textAlign: TextAlign.center,
        ),
        actions: [
          IconButton(
              onPressed: cleanMessageAction,
              icon: const Icon(
                Icons.cleaning_services,
                size: 22,
              )
          ),
          IconButton(
              onPressed: toSettingPage,
              icon: const Icon(
                Icons.settings,
                size: 24,
              )
          )
        ],
      ),
      body: Column(
        children: [
          const HomeHeaderView(),
          Expanded(
            child: Obx(() {
              return ListView.builder(
                itemCount: appController.messageList.length,
                itemBuilder: (context, index) {
                  return HomeListCell(messageModel: appController.messageList[index]);
                }
              );
            }),
          )
        ],
      )
    );
  }
}
